﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.ServiceModel.DomainServices.Client;
using MyBet.Web;
using MyBet.Class;
using MyBet.Helpers;

namespace MyBet.Class
{
    public class MatchsList
    {
        #region Attributes
        private Dictionary<int, MatchControl> _matchDictionnary;
        private StackPanel _parent;
        private EventServiceReference.EventObject _event;
        private DaysList.DayStatus _status;
        private Guid _currentUserId;
        private int _eventId;
        #endregion

        #region Events
        /// <summary>
        /// Event to indicate the list of match is loaded
        /// </summary>
        public delegate void Loaded();
        public event Loaded OnLoaded;
        #endregion

        #region Properties

        public int EventId
        {
            get { return _eventId; }
            set { _eventId = value; }
        }

        public EventServiceReference.EventObject Event
        {
            get { return _event; }
            set { _event = value; }
        }

        public DaysList.DayStatus Status
        {
            get { return _status; }
            set { _status = value; }
        }

        /// <summary>
        /// Container
        /// </summary>
        public StackPanel Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }

        /// <summary>
        /// List of matchs
        /// </summary>
        public Dictionary<int, MatchControl> MatchDictionnary
        {
            get { return _matchDictionnary; }
            set { _matchDictionnary = value; }
        }

        /// <summary>
        /// User to load
        /// </summary>
        public Guid CurrentUserId
        {
            get { return _currentUserId; }
            set { _currentUserId = value; }
        }
        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="parentControl"></param>
        public MatchsList(StackPanel parentControl)
        {
            Parent = parentControl;
            CurrentUserId = Guid.Empty;
            MatchDictionnary = new Dictionary<int, MatchControl>();
            Status = DaysList.DayStatus.NotStarted;
        }

        /// <summary>
        ///  Load championship day
        /// </summary>
        /// <param name="dayNumber">Day to load</param>
        /// <param name="championshipId">Championship</param>
        public void Load(EventServiceReference.EventObject eventToDisplay)
        {
            if (eventToDisplay != null)
            {
                CurrentUserId = Guid.Empty;
                Event = eventToDisplay;
                EventId = Event._eventId;
                EventServiceReference.GetEventScoreClient eventSvc = new EventServiceReference.GetEventScoreClient();
                eventSvc.GetMatchsFromEventCompleted += new EventHandler<EventServiceReference.GetMatchsFromEventCompletedEventArgs>(eventSvc_GetMatchsFromEventCompleted);
                eventSvc.GetMatchsFromEventAsync(EventId);
            }
        }

        /// <summary>
        ///  Load championship day
        /// </summary>
        /// <param name="dayNumber">Day to load</param>
        /// <param name="championshipId">Championship</param>
        public void LoadUserEvent(Guid userId, int eventId)
        {
            CurrentUserId = userId;
            EventId = eventId;
            Event = null;
            EventServiceReference.GetEventScoreClient eventSvc = new EventServiceReference.GetEventScoreClient();
            eventSvc.GetMatchsFromEventCompleted += new EventHandler<EventServiceReference.GetMatchsFromEventCompletedEventArgs>(eventSvc_GetMatchsFromEventCompleted);
            eventSvc.GetMatchsFromEventAsync(eventId);
        }

        void eventSvc_GetMatchsFromEventCompleted(object sender, EventServiceReference.GetMatchsFromEventCompletedEventArgs e)
        {
            if (Parent != null)
            {
                Parent.Children.Clear();
                MatchDictionnary.Clear();
                if (Event != null)
                {
                    Status = DaysList.GetStatus(Event);
                }
                foreach (EventServiceReference.MatchObject m in e.Result)
                {
                    MatchControl mc = new MatchControl();
                    // Display in IHM
                    mc.DayStatus = Status;
                    mc.MatchId = m._matchId;
                    mc.LoadFrom(m);
                    MatchDictionnary.Add(m._matchId, mc);
                    Parent.Children.Add(mc);
                }
            }

            if (CurrentUserId == Guid.Empty)
            {
                if (App.CurrentUser != null)
                {
                    CurrentUserId=App.CurrentUser.UserId;
                }
            }

            if (CurrentUserId != Guid.Empty)
            {
                // Call prediction service to get prediction for all matchs of the current event
                PredictionServiceReference.PredictionServiceClient predictionSvc = new PredictionServiceReference.PredictionServiceClient();
                predictionSvc.GetPredictionCompleted += new EventHandler<PredictionServiceReference.GetPredictionCompletedEventArgs>(predictionSvc_GetPredictionCompleted);
                predictionSvc.GetPredictionAsync(CurrentUserId, EventId);
                return;
            }

            OnLoaded();
        }

        /// <summary>
        ///  Reload championship day
        /// </summary>
        public void Reload()
        {
            Load(Event);
        }

        void predictionSvc_GetPredictionCompleted(object sender, PredictionServiceReference.GetPredictionCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                foreach (PredictionServiceReference.BetObject bet in e.Result)
                {
                    MatchControl mc = MatchDictionnary[bet._matchId];
                    mc.SetBet(bet._teamHomeScore, bet._teamAwayScore);
                }
            }
            OnLoaded();
        }

    }
}
